package com.yuanduo_app.ui.map.model;

import android.os.Bundle;

import cn.ydxlm.maplib.entity.Location;
import com.xujl.fastlib.base.BaseModel;
import com.xujl.fastlib.base.IControl;
import com.xujl.fastlib.utils.NumberUtils;
import com.xujl.fastlib.utils.StringUtils;

import com.yuanduo_app.entity.NearbyEntity;
import com.yuanduo_app.entity.SuggestionEntity;

import java.util.ArrayList;
import java.util.List;

import androidx.databinding.ObservableField;


/**
 * <pre>
 *     author : xujl
 *     e-mail : 597355068@qq.com
 *     time   : 2019/08/20
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class ChooseAddressActivityModel extends BaseModel {
    public ObservableField<String> commitBtnStr = new ObservableField<>();
    public ObservableField<String> searchCity = new ObservableField<>();
    public ObservableField<Boolean> showSuggestion = new ObservableField<>(false);
    private List<SuggestionEntity> mSuggestionEntities = new ArrayList<>();
    private List<NearbyEntity> mNearbyEntities = new ArrayList<>();
    private Location mLocation;

    @Override
    public void init (IControl control) {

    }

    public void saveBundle (Bundle extras) {
        if (extras == null) {
            return;
        }
        commitBtnStr.set("设置为" + extras.getString("type"));
        if (StringUtils.isEmpty(extras.getString("city")) || StringUtils.equals(extras.getString("city"), "undefined")) {
            return;
        }
        mLocation = new Location();
        mLocation.setProvince(extras.getString("province"));
        mLocation.setCity(extras.getString("city"));
        mLocation.setArea(extras.getString("area"));
        mLocation.setLatitude(NumberUtils.doubleOf(extras.getString("lat")));
        mLocation.setLongitude(NumberUtils.doubleOf(extras.getString("lon")));

    }

    public Location getLocation () {
        return mLocation;
    }

    public void setLocation (Location location) {
        if (location == null) {
            mLocation = new Location();
            return;
        }
        mLocation = location;
    }

    public List<SuggestionEntity> getSuggestionEntities () {
        return mSuggestionEntities;
    }

    public List<NearbyEntity> getNearbyEntities () {
        return mNearbyEntities;
    }

    public void refreshSuggestionList (List<Location> list) {
        mSuggestionEntities.clear();
        for (Location location : list) {
            mSuggestionEntities.add(new SuggestionEntity(location));
        }
    }

    public void refreshPoiList (List<Location> data) {
        mNearbyEntities.clear();
        for (Location location : data) {
            mNearbyEntities.add(new NearbyEntity(location));
        }
    }

    public void refreshPoiSuggestionList (List<SuggestionEntity> suggestionEntities, int position) {
        mNearbyEntities.clear();
        for (SuggestionEntity entity : suggestionEntities) {
            mNearbyEntities.add(new NearbyEntity(entity.getLocation(), suggestionEntities.indexOf(entity) == position));
        }
    }

    public void changeNearbyChecked (int position) {
        for (int i = 0; i < mNearbyEntities.size(); i++) {
            mNearbyEntities.get(i).setChecked(i == position);
        }
    }

    public Location getCheckedAddress () {
        for (NearbyEntity entity : mNearbyEntities) {
            if (entity.isChecked()) {
                return entity.getLocation();
            }
        }
        return null;
    }


}
